package com.example.mybatisplusdemo.Handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatisplus字段自动填充
 *
 * @author jiyun
 */

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //新增操作
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.getValue("createTime") == null) {
            this.setFieldValByName("createTime", new Date(), metaObject);
        }
        if (metaObject.getValue("createBy") == null) {
            this.setFieldValByName("createBy", "admin", metaObject);
        }
        if (metaObject.getValue("delFlag") == null) {
            this.setFieldValByName("delFlag", "0", metaObject);
        }
    }

    //修改操作
    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.getValue("updateTime") == null) {
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
        if (metaObject.getValue("updateBy") == null) {
            this.setFieldValByName("updateBy", "admin", metaObject);
        }
    }
}